package jp.joogoo.web.model;

import java.io.Serializable;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;
import org.slim3.datastore.ModelRef;
import org.slim3.datastore.Sort;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.SortDirection;

@Model(schemaVersion = 1)
public class M_joogoo implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key; // 自動生成
    @Attribute(version = true)
    private Long version; // バージョンNo(多重更新検知用)
    private String userId; // ユーザーID（Googleアカウント）
    private ModelRef<M_service> mServiceRef = new ModelRef<M_service>(
        M_service.class); // M_serviceへの参照
    private String token; // トークン
    private int interval; // 更新間隔(単位：分)
    @Attribute(persistent = false)
    private InverseModelListRef<T_new, M_joogoo> newListRef =
        new InverseModelListRef<T_new, M_joogoo>(
            T_new.class,
            "mJoogooRef",
            this,
            new Sort("createdAt", SortDirection.ASCENDING));

    /**
     * Returns the key.
     * 
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     * 
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     * 
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     * 
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public ModelRef<M_service> getMServiceRef() {
        return mServiceRef;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        M_joogoo other = (M_joogoo) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    public InverseModelListRef<T_new, M_joogoo> getNewListRef() {
        return newListRef;
    }
}